草庐IT

JavaScript setInterval 和 `this` 解决方案

全部标签

javascript - 我正在尝试创建一个带有指针的 substr 方法……有更优雅的解决方案吗?

这是交易。我正在做一些字符串操作,我经常使用substr方法。但是,我需要使用它的方式更像是一种phpfread方法。然而,我的substr需要由指针引导。该过程需要像这样:varstring='Loremipsumdolorsitamet,consectetur'如果我读入,'Lorem'.....作为我的第一个substr调用:string.substr(offset,strLenth)//0,5然后我的下一个substr调用应该自动从我字符串中的这个位置开始的偏移量开始:offsetpointerstartsherenow=>ipsumdolorsitamet,consectet

javascript - jquery: this.not (':animated' ) && that.is (':visible' ) 不遵守规则,语法问题?只有几行代码

当我点击#button时,它仍在执行'dosomething',即使.wrapper是动画并且.wrapperspan不可见。所以它不遵守规则。怎么了?$('#button').click(function(){if($('.wrapper').not(':animated')&&$('.wrapperspan').is(':visible')){//dosomething}}) 最佳答案 如果没有if语句,这样会更简洁一些。workingdemo$('#button').click(function(){$('.wrapper')

javascript - 通过 "this"和 "prototype"分配函数有什么区别?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Useof'prototype'vs.'this'inJavascript?我对这两种向函数添加方法感到困惑。让我用一个例子来解释。varfoo=function(){this.bar=function(){alert('Iamamethod')}}foo.prototype.baz=function(){alert('Iamanothermethod')}varcar=newfoo();此时我们可以对汽车使用baz和bar方法。好吧,但是它们之间有什么区别。向函数的原型(prototype)或其构造函数添加

javascript - 在 Javascript 闭包中访问 'this'

这更像是一种完整性检查。我发现在Javascript中使用闭包时,我经常使用以下模式从函数内部访问封闭类:MyClass.prototype.delayed_foo=function(){varself=this;setTimeout(function(){self.foo();//BeniceifIcoulduse'this'here},1000);};显然这工作得很好,而且使用起来也不是什么大麻烦。我脑子里有个小痒痒在说“你把事情搞得太复杂了,笨蛋!”这是普遍接受的模式吗? 最佳答案 这是普遍接受的模式,但通常使用that而不是

javascript - 调用 Function.prototype.method() 时 TypeError : this. prototype is undefined

我正在读《Javascript:好的部分》这本书。现在我正在阅读有关增强类型的章节:Function.prototype.method=function(name,func){this.prototype[name]=func;returnthis;};更新:为什么下面的代码不起作用?js>Function.prototype.method("test",function(){print("TEST")});typein:2:TypeError:this.prototypeisundefined但是下面的代码没有问题:js>Function.method("test",function

javascript - jquery.each 函数是否有可能不破坏 'this' 变量?

所以如果变量“this”当前被设置为一个对象,{name:"Theoldthis"}下面的代码会在循环中改变它vararray=[1,2,3];$.each(array,function(i,e){alert(this.name);});不会找到this.name,而是在循环执行期间将变量“this”设置为与“e”相同是否可以让jquery不破坏$.each循环中的this变量? 最佳答案 如果您使用native.forEach而不是$.each,您可以通过发送第二个回调来设置回调的this值争论...array.forEach(f

十分钟 Javascript : What is going on in this example code illustrating lazy scoping?

我一直在重读SpencerTipping的优秀作品JavascriptinTenMinutes在这个使用惰性作用域创建语法宏的示例中,我终究无法弄清楚发生了什么:varf=function(){return$0+$1};varg=eval(f.toString().replace(/\$(\d+)/g,function(_,digits){return'arguments['+digits+']'}));g(5,6);//=>11(exceptonIE)特别是,$0和$1正在被一个函数定义取代——那个函数是如何被计算的?(大概是通过eval(),但我没有看到)。函数中单个下划线参数的用

javascript - "this"的范围

我有一个简单的对象,我不明白this的概念(作用域)是通过调用这个对象的函数来实现的。为什么在最后一个变体(3)中调用show()(使用函数show()insideobjectwithoutparent)结果是“Thisisglobal”并且不是内部变量title("ColorPicker")?我有一个模糊的想法,即在定义全局变量show之后调用函数popup.show(),this指的是全局对象。这是逻辑解释吗?代码:http://jsbin.com/otuzac/1/edit.vartitle="'This'isglobal";varpopup={dom_element:("#po

javascript - 是否有扩展核心 js 类的任何好的 underscore.js 替代方案?

我发现_.something(somevar,some_function_or_other_thing);“语法”非常难看。使用类似ruby​​的迭代器和类似东西的一些好的替代方案是什么:10..times(function(i){console.log(i);});uppercasefoobar=["foo","bar"].each(function(i){returni.toUpperCase();});此外,我正在使用node.js,因此它应该更多地关注代码而不是DOM内容。 最佳答案 很惊讶没有人提到Lo-Dash.Lo-D

javascript - 为什么使用 .call(this) 而不是括号

这个问题在这里已经有了答案:Reasonbehindthisselfinvokinganonymousfunctionvariant(5个答案)关闭8年前。有没有什么特别的原因让我经常遇到:(function(){console.log("Hello");}).call(this);代替:(function(){console.log("Hello");})();传不传this调用应该是一样的效果吧?似乎有一些性能差异:http://jsperf.com/call-vs-parenthesis.